import { defineStore } from 'pinia'

interface Content {
  page: {
    article: number
    source: number
  }
  content: {
    articles: App.Article[]
    sources: App.Source[]
  }
}

// user info
interface UserBaseInfo {
  username: string
  sex: boolean
  sign: string
  birthday: number
  location: string
  education: string
  univesity: string
  avatar: string
  articleNum: number
  sourceNum: number
  workNum: number
  followNum: number
  followedNum: number
}

interface UserAccountInfo {
  email: string
  password: string
}

type UserInfo = UserBaseInfo & UserAccountInfo

// user state
export interface UserStateLogin {
  isLogin: true
  ifNewMessage: boolean
  info: UserInfo
  work: {
    article: App.Article[]
    source: App.Source[]
  }
  collection: {
    article: App.Article[]
    source: App.Source[]
  }
  history: {
    article: App.Article[]
    source: App.Source[]
  }
}

export interface UserStateUnLogin {
  isLogin: false
}

export type UserState = UserStateLogin | UserStateUnLogin

function generateContent(): Content {
  let content: Content = {
    page: {
      article: 0,
      source: 0,
    },
    content: {
      articles: [],
      sources: [],
    },
  }
  return content
}

export const useUserStore = defineStore('user', {
  state: (): UserState => {
    let userState = {
      isLogin: true,
    } as UserState
    return userState
  },
  getters: {
    userBaseInfos(state): UserBaseInfo | undefined {
      let baseInfoKeyList = [
        'username',
        'nickname',
        'sex',
        'sign',
        'birthday',
        'location',
        'education',
        'university',
      ]
      if (!state.isLogin) return undefined
      let userInfos = state.info as any
      let obj = {} as any
      Object.keys(userInfos).forEach((key) => {
        if (baseInfoKeyList.includes(key)) {
          obj[key] = userInfos[key]
        }
      })
      return obj
    },
    userAccountInfos(state): UserAccountInfo | undefined {
      let accountUserInfoKeyList = ['email', 'password']
      if (!state.isLogin) return
      let userInfos = state.info as any
      let obj = {} as any
      Object.keys(userInfos).forEach((key) => {
        if (accountUserInfoKeyList.includes(key)) {
          obj[key] = userInfos[key]
        }
      })
      return obj
    },
  },
  actions: {
    login() {
      let state = this.$state
      state.isLogin = true
      ;['history', 'collection', 'work'].forEach((k) => {
        ;(state as any)[k] = generateContent()
      })
    },
    welcome() {
      console.log('UserStore: welcome')
    },
    cleanHistorySource() {
      console.log('clean history source')
    },
    cleanHistoryArticle() {
      console.log('clean history article')
    },
    logout() {
      console.log('logout')
    },
    fetchUserInfos() {
      console.log('fetch user infos')
    },
    changeAccountInfo(option: any) {
      console.log('change account info')
    },
    changeBaseInfo(optoin: any) {
      console.log('change base info')
    },
    fetchHistoryArticleAbstracts() {
      console.log('fetch history article abstract')
    },
    fetchWorksArticleAbstracts() {
      console.log('fetchWorksArticleAbstracts')
    },
    fetchCollectionArticleAbstracts() {
      console.log('fetchCollectionArticleAbstracts')
    },
  },
})
